﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Prm.System
{
    public class SubstanceManager : AbpZeroTemplateDomainServiceBase
    {
        public Abp.Runtime.Session.IAbpSession AbpSession { get; set; }
        public IRepository<Substance, int> substanceRepository { get; private set; }
        public IRepository<Product, int> productRepository { get; private set; }

        public SubstanceManager(IRepository<Substance, int> _substanceRepository, IRepository<Product, int> _productRepository)
        {
            substanceRepository = _substanceRepository;
            productRepository = _productRepository;
            AbpSession = Abp.Runtime.Session.NullAbpSession.Instance;
        }




        public async Task SetSubstanceProduct(Substance substance, Product product)
        {
            var sub = new Substance();
            if (substance.Id > 0)
            {
                sub = await substanceRepository.GetAsync(substance.Id);
            }
            else
            {
                await substanceRepository.InsertAsync(sub);
            }
            sub.Formula = substance.Formula;
            sub.EnName = substance.EnName;
            sub.CnName = substance.CnName;
            sub.HighRisk = substance.HighRisk;
            sub.Note = substance.Note;
            sub.CAS = substance.CAS;
            sub.MolecularWeight = substance.MolecularWeight;

            if (product.Id == 0)
            {
                var p = new Product()
                {
                    Code = product.Code,
                    SubstanceId = sub.Id
                };
                await productRepository.InsertAsync(p);
            }
        }



    }
}
